<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI聊天助手</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            height: 100vh;
            overflow: hidden;
        }

        .chat-container {
            display: flex;
            height: 100vh;
            max-width: 1400px;
            margin: 0 auto;
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(20px);
            border-radius: 20px;
            margin: 20px;
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.25);
            overflow: hidden;
        }

        .sidebar {
            width: 320px;
            background: linear-gradient(180deg, #2d3748 0%, #1a202c 100%);
            color: white;
            display: flex;
            flex-direction: column;
            border-radius: 20px 0 0 20px;
        }

        .sidebar-header {
            padding: 25px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }

        .sidebar-title {
            font-size: 24px;
            font-weight: 700;
            margin-bottom: 8px;
            background: linear-gradient(45deg, #4299e1, #9f7aea);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        .sidebar-subtitle {
            font-size: 14px;
            opacity: 0.7;
        }

        .model-config {
            padding: 20px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }

        .config-group {
            margin-bottom: 15px;
        }

        .config-label {
            font-size: 12px;
            font-weight: 600;
            margin-bottom: 5px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            opacity: 0.8;
        }

        .config-input, .config-select {
            width: 100%;
            padding: 10px 12px;
            border: 1px solid rgba(255, 255, 255, 0.2);
            border-radius: 8px;
            background: rgba(255, 255, 255, 0.1);
            color: white;
            font-size: 14px;
            transition: all 0.3s ease;
        }

        .config-input:focus, .config-select:focus {
            outline: none;
            border-color: #4299e1;
            background: rgba(255, 255, 255, 0.15);
            box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.1);
        }

        .config-input::placeholder {
            color: rgba(255, 255, 255, 0.5);
        }

        .mcp-section {
            padding: 20px;
        }

        .mcp-toggle {
            display: flex;
            align-items: center;
            margin-bottom: 15px;
        }

        .toggle-switch {
            position: relative;
            width: 50px;
            height: 25px;
            background: rgba(255, 255, 255, 0.2);
            border-radius: 25px;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-right: 10px;
        }

        .toggle-switch.active {
            background: #4299e1;
        }

        .toggle-slider {
            position: absolute;
            top: 2px;
            left: 2px;
            width: 21px;
            height: 21px;
            background: white;
            border-radius: 50%;
            transition: all 0.3s ease;
        }

        .toggle-switch.active .toggle-slider {
            transform: translateX(25px);
        }

        .main-chat {
            flex: 1;
            display: flex;
            flex-direction: column;
            background: white;
        }

        .chat-header {
            padding: 25px 30px;
            border-bottom: 1px solid #e2e8f0;
            background: linear-gradient(90deg, #f7fafc 0%, #edf2f7 100%);
        }

        .chat-title {
            font-size: 20px;
            font-weight: 700;
            color: #2d3748;
            margin-bottom: 5px;
        }

        .chat-status {
            font-size: 14px;
            color: #718096;
            display: flex;
            align-items: center;
        }

        .status-dot {
            width: 8px;
            height: 8px;
            border-radius: 50%;
            background: #48bb78;
            margin-right: 8px;
            animation: pulse 2s infinite;
        }

        @keyframes pulse {
            0%, 100% { opacity: 1; }
            50% { opacity: 0.5; }
        }

        .messages-container {
            flex: 1;
            overflow-y: auto;
            padding: 20px 30px;
            background: linear-gradient(180deg, #ffffff 0%, #f7fafc 100%);
        }

        .message {
            margin-bottom: 20px;
            animation: fadeInUp 0.5s ease-out;
        }

        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .message-header {
            display: flex;
            align-items: center;
            margin-bottom: 8px;
        }

        .message-avatar {
            width: 32px;
            height: 32px;
            border-radius: 50%;
            margin-right: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 600;
            font-size: 14px;
        }

        .user-avatar {
            background: linear-gradient(45deg, #4299e1, #9f7aea);
            color: white;
        }

        .ai-avatar {
            background: linear-gradient(45deg, #48bb78, #38b2ac);
            color: white;
        }

        .message-name {
            font-weight: 600;
            color: #2d3748;
            margin-right: 10px;
        }

        .message-time {
            font-size: 12px;
            color: #a0aec0;
        }

        .message-content {
            margin-left: 44px;
            padding: 15px 20px;
            border-radius: 16px;
            line-height: 1.6;
            word-wrap: break-word;
        }

        .user-message {
            background: linear-gradient(135deg, #4299e1 0%, #9f7aea 100%);
            color: white;
            margin-left: 80px;
        }

        .ai-message {
            background: #f7fafc;
            color: #2d3748;
            border: 1px solid #e2e8f0;
        }

        .input-container {
            padding: 20px 30px;
            border-top: 1px solid #e2e8f0;
            background: white;
        }

        .input-wrapper {
            display: flex;
            align-items: flex-end;
            background: #f7fafc;
            border-radius: 16px;
            padding: 12px;
            border: 2px solid transparent;
            transition: all 0.3s ease;
        }

        .input-wrapper:focus-within {
            border-color: #4299e1;
            box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.1);
        }

        .message-input {
            flex: 1;
            border: none;
            background: transparent;
            padding: 8px 12px;
            font-size: 16px;
            color: #2d3748;
            resize: none;
            max-height: 120px;
            min-height: 20px;
            outline: none;
        }

        .message-input::placeholder {
            color: #a0aec0;
        }

        .send-button {
            padding: 10px 16px;
            background: linear-gradient(45deg, #4299e1, #9f7aea);
            color: white;
            border: none;
            border-radius: 12px;
            cursor: pointer;
            font-weight: 600;
            margin-left: 10px;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .send-button:hover:not(:disabled) {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(66, 153, 225, 0.3);
        }

        .send-button:disabled {
            opacity: 0.5;
            cursor: not-allowed;
            transform: none;
        }

        .typing-indicator {
            display: none;
            margin-left: 44px;
            padding: 15px 20px;
            background: #f7fafc;
            border-radius: 16px;
            border: 1px solid #e2e8f0;
        }

        .typing-dots {
            display: flex;
            gap: 4px;
        }

        .typing-dot {
            width: 8px;
            height: 8px;
            border-radius: 50%;
            background: #a0aec0;
            animation: typing 1.4s infinite ease-in-out;
        }

        .typing-dot:nth-child(2) { animation-delay: 0.2s; }
        .typing-dot:nth-child(3) { animation-delay: 0.4s; }

        @keyframes typing {
            0%, 60%, 100% { transform: translateY(0); }
            30% { transform: translateY(-10px); }
        }

        .error-message {
            background: #fed7d7;
            color: #c53030;
            border: 1px solid #feb2b2;
        }

        @media (max-width: 768px) {
            .chat-container {
                margin: 10px;
                border-radius: 15px;
            }
            
            .sidebar {
                width: 280px;
            }
            
            .user-message {
                margin-left: 40px;
            }
        }
    </style>
</head>
<body>
    <div class="chat-container">
        <div class="sidebar">
            <div class="sidebar-header">
                <div class="sidebar-title">AI 聊天助手</div>
                <div class="sidebar-subtitle">智能对话 · 无限可能</div>
            </div>
            
            <div class="model-config">
                <div class="config-group">
                    <div class="config-label">模型提供商</div>
                    <select class="config-select" id="modelProvider">
                        <option value="openai">OpenAI</option>
                        <option value="anthropic">Anthropic</option>
                        <option value="gemini">Google Gemini</option>
                        <option value="claude">Claude</option>
                        <option value="custom">自定义</option>
                    </select>
                </div>
                
                <div class="config-group">
                    <div class="config-label">API 密钥</div>
                    <input type="password" class="config-input" id="apiKey" placeholder="输入你的API密钥">
                </div>
                
                <div class="config-group">
                    <div class="config-label">模型名称</div>
                    <input type="text" class="config-input" id="modelName" placeholder="gpt-4, claude-3-sonnet等">
                </div>
                
                <div class="config-group">
                    <div class="config-label">API 端点 (可选)</div>
                    <input type="text" class="config-input" id="apiEndpoint" placeholder="https://api.openai.com/v1">
                </div>
            </div>
            
            <div class="mcp-section">
                <div class="config-label">MCP 集成</div>
                <div class="mcp-toggle">
                    <div class="toggle-switch" id="mcpToggle">
                        <div class="toggle-slider"></div>
                    </div>
                    <span>启用MCP服务器</span>
                </div>
                
                <div class="config-group">
                    <div class="config-label">MCP 服务器地址</div>
                    <input type="text" class="config-input" id="mcpServer" placeholder="ws://localhost:3000" disabled>
                </div>
                
                <div class="config-group">
                    <div class="config-label">MCP 工具</div>
                    <select class="config-select" id="mcpTools" disabled>
                        <option value="">选择可用工具</option>
                        <option value="filesystem">文件系统</option>
                        <option value="database">数据库</option>
                        <option value="web">网页浏览</option>
                        <option value="calculator">计算器</option>
                    </select>
                </div>
            </div>
        </div>
        
        <div class="main-chat">
            <div class="chat-header">
                <div class="chat-title">智能对话</div>
                <div class="chat-status">
                    <div class="status-dot"></div>
                    <span id="statusText">准备就绪</span>
                </div>
            </div>
            
            <div class="messages-container" id="messagesContainer">
                <div class="message">
                    <div class="message-header">
                        <div class="message-avatar ai-avatar">AI</div>
                        <div class="message-name">AI助手</div>
                        <div class="message-time">刚刚</div>
                    </div>
                    <div class="message-content ai-message">
                        你好！我是你的AI助手。我已经准备好与你对话了。你可以在左侧配置你想要使用的大模型，也可以启用MCP来获得更多功能。有什么我可以帮助你的吗？
                    </div>
                </div>
            </div>
            
            <div class="typing-indicator" id="typingIndicator">
                <div class="typing-dots">
                    <div class="typing-dot"></div>
                    <div class="typing-dot"></div>
                    <div class="typing-dot"></div>
                </div>
            </div>
            
            <div class="input-container">
                <div class="input-wrapper">
                    <textarea class="message-input" id="messageInput" placeholder="输入你的消息..." rows="1"></textarea>
                    <button class="send-button" id="sendButton">
                        <span>发送</span>
                        <span>⤴</span>
                    </button>
                </div>
            </div>
        </div>
    </div>

    <script>
        class ChatApp {
            constructor() {
                this.messages = [];
                this.config = {
                    provider: 'openai',
                    apiKey: '',
                    modelName: 'gpt-3.5-turbo',
                    apiEndpoint: '',
                    mcpEnabled: false,
                    mcpServer: '',
                    mcpTools: ''
                };
                
                this.initElements();
                this.bindEvents();
                this.loadConfig();
                this.autoResizeTextarea();
            }
            
            initElements() {
                this.messagesContainer = document.getElementById('messagesContainer');
                this.messageInput = document.getElementById('messageInput');
                this.sendButton = document.getElementById('sendButton');
                this.typingIndicator = document.getElementById('typingIndicator');
                this.statusText = document.getElementById('statusText');
                
                // Config elements
                this.modelProvider = document.getElementById('modelProvider');
                this.apiKey = document.getElementById('apiKey');
                this.modelName = document.getElementById('modelName');
                this.apiEndpoint = document.getElementById('apiEndpoint');
                this.mcpToggle = document.getElementById('mcpToggle');
                this.mcpServer = document.getElementById('mcpServer');
                this.mcpTools = document.getElementById('mcpTools');
            }
            
            bindEvents() {
                this.sendButton.addEventListener('click', () => this.sendMessage());
                this.messageInput.addEventListener('keypress', (e) => {
                    if (e.key === 'Enter' && !e.shiftKey) {
                        e.preventDefault();
                        this.sendMessage();
                    }
                });
                
                // Config events
                this.modelProvider.addEventListener('change', () => this.updateConfig());
                this.apiKey.addEventListener('input', () => this.updateConfig());
                this.modelName.addEventListener('input', () => this.updateConfig());
                this.apiEndpoint.addEventListener('input', () => this.updateConfig());
                
                // MCP toggle
                this.mcpToggle.addEventListener('click', () => this.toggleMCP());
                this.mcpServer.addEventListener('input', () => this.updateConfig());
                this.mcpTools.addEventListener('change', () => this.updateConfig());
            }
            
            autoResizeTextarea() {
                this.messageInput.addEventListener('input', () => {
                    this.messageInput.style.height = 'auto';
                    this.messageInput.style.height = Math.min(this.messageInput.scrollHeight, 120) + 'px';
                });
            }
            
            toggleMCP() {
                this.config.mcpEnabled = !this.config.mcpEnabled;
                this.mcpToggle.classList.toggle('active', this.config.mcpEnabled);
                this.mcpServer.disabled = !this.config.mcpEnabled;
                this.mcpTools.disabled = !this.config.mcpEnabled;
                this.saveConfig();
                this.updateStatus();
            }
            
            updateConfig() {
                this.config.provider = this.modelProvider.value;
                this.config.apiKey = this.apiKey.value;
                this.config.modelName = this.modelName.value;
                this.config.apiEndpoint = this.apiEndpoint.value;
                this.config.mcpServer = this.mcpServer.value;
                this.config.mcpTools = this.mcpTools.value;
                this.saveConfig();
                this.updateStatus();
            }
            
            saveConfig() {
                // In a real implementation, you might save to localStorage
                // localStorage.setItem('chatConfig', JSON.stringify(this.config));
            }
            
            loadConfig() {
                // In a real implementation, you might load from localStorage
                // const saved = localStorage.getItem('chatConfig');
                // if (saved) {
                //     this.config = { ...this.config, ...JSON.parse(saved) };
                //     this.updateUI();
                // }
                this.updateStatus();
            }
            
            updateStatus() {
                let status = '准备就绪';
                if (!this.config.apiKey) {
                    status = '请配置API密钥';
                } else if (this.config.mcpEnabled && !this.config.mcpServer) {
                    status = 'MCP已启用，请配置服务器';
                } else if (this.config.mcpEnabled) {
                    status = `已连接 (MCP: ${this.config.mcpTools || '无工具'})`;
                }
                this.statusText.textContent = status;
            }
            
            async sendMessage() {
                const content = this.messageInput.value.trim();
                if (!content) return;
                
                if (!this.config.apiKey) {
                    this.addMessage('system', '请先在左侧配置API密钥', true);
                    return;
                }
                
                this.messageInput.value = '';
                this.messageInput.style.height = 'auto';
                this.sendButton.disabled = true;
                
                // Add user message
                this.addMessage('user', content);
                
                // Show typing indicator
                this.showTyping(true);
                
                try {
                    // Simulate API call
                    const response = await this.callAPI(content);
                    this.addMessage('assistant', response);
                } catch (error) {
                    this.addMessage('assistant', '抱歉，处理你的请求时出现了错误。请检查配置并重试。', true);
                } finally {
                    this.showTyping(false);
                    this.sendButton.disabled = false;
                }
            }
            
            async callAPI(message) {
                // Simulate API call delay
                await new Promise(resolve => setTimeout(resolve, 1000 + Math.random() * 2000));
                
                // This is a mock response - in real implementation, you would call your chosen AI API
                const responses = [
                    `我理解你的问题："${message}"。这是一个很有趣的话题！`,
                    `基于你的输入，我认为可以从以下几个角度来分析...`,
                    `谢谢你的分享！让我来详细回答你的问题。`,
                    `这是一个很好的问题。根据我的理解...`,
                    `我很乐意帮助你解决这个问题！`
                ];
                
                const baseResponse = responses[Math.floor(Math.random() * responses.length)];
                
                if (this.config.mcpEnabled && this.config.mcpTools) {
                    return baseResponse + `\n\n*注意：我已经通过MCP工具"${this.config.mcpTools}"增强了我的能力，可以为你提供更准确和实用的帮助。*`;
                }
                
                return baseResponse;
            }
            
            addMessage(role, content, isError = false) {
                const message = {
                    role,
                    content,
                    timestamp: new Date(),
                    isError
                };
                
                this.messages.push(message);
                this.renderMessage(message);
                this.scrollToBottom();
            }
            
            renderMessage(message) {
                const messageDiv = document.createElement('div');
                messageDiv.className = 'message';
                
                const isUser = message.role === 'user';
                const avatar = isUser ? 'U' : (message.role === 'system' ? 'S' : 'AI');
                const name = isUser ? '你' : (message.role === 'system' ? '系统' : 'AI助手');
                const avatarClass = isUser ? 'user-avatar' : 'ai-avatar';
                const messageClass = isUser ? 'user-message' : 'ai-message';
                
                messageDiv.innerHTML = `
                    <div class="message-header">
                        <div class="message-avatar ${avatarClass}">${avatar}</div>
                        <div class="message-name">${name}</div>
                        <div class="message-time">${this.formatTime(message.timestamp)}</div>
                    </div>
                    <div class="message-content ${messageClass} ${message.isError ? 'error-message' : ''}">${message.content}</div>
                `;
                
                this.messagesContainer.appendChild(messageDiv);
            }
            
            showTyping(show) {
                this.typingIndicator.style.display = show ? 'block' : 'none';
                if (show) {
                    this.scrollToBottom();
                }
            }
            
            scrollToBottom() {
                setTimeout(() => {
                    this.messagesContainer.scrollTop = this.messagesContainer.scrollHeight;
                }, 100);
            }
            
            formatTime(date) {
                return date.toLocaleTimeString('zh-CN', { 
                    hour: '2-digit', 
                    minute: '2-digit' 
                });
            }
        }
        
        // Initialize the app
        document.addEventListener('DOMContentLoaded', () => {
            new ChatApp();
        });
    </script>
</body>
</html>